/**
 * Created by zhuwl on 2017/9/22 0022.
 */
new Vue({
    el: "#app",
    data: {
        totalMoney: 0,
        productList: [],
        check: true
    },
    mounted: function () {
        this.$nextTick(function () {
            console.log("项目实例化结束");
            this.cartView();
        })
    },
    filters: {
        formatMoney: function (val, format) {
            return format + Number(val).toFixed(2);
        }
    },
    computed: {
        computedTotalMoney: function () {
            let totalMoneyTemp = 0;
            this.productList.forEach(function (val) {
                if (val.check) {
                    totalMoneyTemp += Number(val.productQuantity * val.productPrice);
                }
            });
            this.totalMoney = totalMoneyTemp;
        }
    },
    methods: {
        cartView: function () {
            this.$http.get("data/cartData.json", {}).then(res => {
                console.log("获取的购物车列表");
                console.log(res.data.result);
                this.productList = res.data.result.list;
                this.totalMoney = res.data.result.totalMoney;
                this.check = res.data.result.check;
            })
        },
        add: function (item) {
            item.productQuantity++;
            this.computedTotalMoney;
        },
        minus: function (item) {
            if (item.productQuantity > 0) {
                item.productQuantity--;
                this.computedTotalMoney;
            }

        },
        //单选
        chooseOne: function (item) {
            item.check = !item.check;
            this.computedTotalMoney;
        },
        chooseAll: function (flag) {
            this.productList.forEach(function (val) {
                if (flag) {
                    this.check = val.check = true;
                } else {
                    this.check = val.check = false;
                }
                console.log(this.check)
            });
            this.computedTotalMoney;
        }
    }
});